#include <system.h>


char* itoa(char* str, int num, unsigned char base)
{
 char i = 0, j = 0, neg = 0;
 int divisor = 1;

 if(base == 0) base = 10;

 if(num < 0) neg = 1, num = -num;

 for(; j < 31; j++)
 {
  if(num >= divisor)
  {
   char d = ((num / divisor) % base);
   str[i] = (d < 10) ?  d + '0' : d - 10 + 'A';
   divisor *= base;
   i++;
  }else break;
 }

 if(i == 0) str[i] = '0', i++;

 // Add a negative sign, if necessary
 if(neg) str[i] = '-', i++;

 // Reverse the string
 for(j = 0; j < i/2; j++)
 {
  char t = str[j];
  str[j] = str[i-j-1];
  str[i-j-1] = t;
 }

 str[i] = 0; // NULL-terminate the string

 return str;
}